﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;

namespace Lead2012.Utility
{
    public partial class ValidateInput
    {
        /// <summary>
        /// Check Validate Email
        /// </summary>
        public static bool IsValidEmailAddress(string EmailAddress)
        {
            Regex regEmail = new Regex(@"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");

            if (regEmail.IsMatch(EmailAddress))
                return true;

            return false;
        }
        public static bool IsContain(string svalue, string[] arr)
        {
            bool valid = false;

            for (int i = 0; i < arr.Length; i++)
            {
                if (StringClearFormat.ClearCharacterSpecialOutput(svalue).ToUpper().Contains(arr[i].ToUpper()))
                {
                    valid = true;
                    break;
                }
            }


            return valid;
        }
        /// <summary>
        /// Return value are true or false
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool IsImage(string fileName)
        {
            if (fileName != null
                && (fileName.ToLower().IndexOf(".jpeg") > -1 || fileName.ToLower().IndexOf(".jpg") > -1 || fileName.ToLower().IndexOf(".gif") > -1 || fileName.ToLower().IndexOf(".png") > -1)
                && (fileName.Length >= 1 && fileName.Length < 200))
            {
                return true;
            }
            return false;
        }
        public static bool IsFlash(string fileName)
        {
            if (fileName != null
                && (fileName.IndexOf(".swf") > -1)
                && (fileName.Length > 5 && fileName.Length < 200))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// Clear all character input incorrect.
        /// </summary>
        /// <param name="inputText">return a string input valid</param>
        /// <returns></returns>
        public static string InputText(string inputText)
        {
            return inputText;
        }

        public static bool isBool(string inputText)
        {
            try
            {
                return bool.Parse(inputText);
            }
            catch
            {
                return false;
            }

        }
        public static bool IsNumber(string pText)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(pText);
        }
        public static DateTime IsDateTime(string intputDate)
        {
            try
            {
                return DateTime.Parse(intputDate);
            }
            catch
            {
                return DateTime.Now;
            }
        }
        public static DateTime IsShortDate(string inputText)
        {
            try
            {
                return DateTime.Parse(DateTime.Parse(inputText).ToShortDateString());
            }
            catch
            {
                return DateTime.Parse(DateTime.Now.ToShortDateString());
            }
        }

        public static int isInt(string inputText)
        {
            try
            {
                return int.Parse(inputText);
            }
            catch
            {
                return 0;
            }
        }

        public static decimal isDecimal(string inputText)
        {
            try
            {
                return decimal.Parse(inputText);
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// Clear all character not allow.
        /// </summary>
        /// <param name="inputText">return a string</param>
        public static string ClearInputText(string inputText)
        {
            if (!string.IsNullOrEmpty(inputText))
            {
                inputText = System.Text.RegularExpressions.Regex.Replace(inputText, "[^0-9a-zA-Z]+?", "");
            }

            return inputText;
        }

        public static string RemoveHtml(string html)
        {
            if (html == null || html == string.Empty)
                return string.Empty;
            html = Regex.Replace(html, "<title.*?</title>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "<object.*?</object>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "<script.*?</script>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "<style.*?</style>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "<w.*?</w.*?>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("<[^>]*>");
            html = rx.Replace(html, string.Empty);
            return html;
        }

        public static string RemoveScriptHtml(string html)
        {
            if (html == null || html == string.Empty)
                return string.Empty;
            html = Regex.Replace(html, "<title.*?</title>", string.Empty, RegexOptions.Singleline | RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "<object.*?</object>", string.Empty, RegexOptions.Singleline | RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "<script.*?</script>", string.Empty, RegexOptions.Singleline | RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "<style.*?</style>", string.Empty, RegexOptions.Singleline | RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "<w.*?</w.*?>", string.Empty, RegexOptions.Singleline | RegexOptions.IgnoreCase);
            return html;
        }
        public static bool CheckYahoo(string yahooName)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://opi.yahoo.com/online?u=" + yahooName.ToLower() + "&m=s&t=8"); // setting up connection with web and posting
                HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Returns httpwebresponse
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string readerYahoo = reader.ReadToEnd().ToLower();
                readerYahoo = readerYahoo.Replace(yahooName.ToLower(), string.Empty);
                if (readerYahoo.Contains("not"))
                    return false;
                else
                    return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
